package com.jhx.video.publish

import android.text.TextUtils
import com.jhx.common.app.CommonModule
import com.jhx.common.http.ApiManager
import com.jhx.common.http.HttpRequest
import com.jhx.common.http.HttpResponse
import com.jhx.common.http.ResponseCallback
import com.jhx.common.model.Video
import com.jhx.common.thirdparty.db.DBHelper
import com.jhx.video.model.Bonus
import com.jhx.video.service.VideoService
import com.jhx.video.videoupload.VideoPublish
import com.jhx.video.videoupload.TXUGCPublishTypeDef
import retrofit2.Call

/**
 *
 * @author Jack 2018-03-08 18:57
 */
class PublishPresenterImpl : Contract.PublishPresenter() {
    private var mCall: Call<HttpResponse<Any>>? = null
    private var mPublishCall: Call<HttpResponse<Bonus>>? = null

    override fun recycler() {
        mView = null
        mCall?.cancel()
        mPublishCall?.cancel()
    }

    override fun getSign() {
        mView?.showLoading()
        mCall = ApiManager.get().getApiService(VideoService::class.java).getSignature()
        HttpRequest.getRequest().autoRecycle(mView?.getContext())
                .start(mCall, object: ResponseCallback<Any>() {
                    override fun onSuccess(url: String?, data: Any?) {
                        mView?.hideLoading()
                        mView?.getSignSuccess("${(data as? Map<*, *>)?.get("sing")}")
                    }

                    override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                        super.onError(url, errorCode, errorMsg)
                        mView?.hideLoading()
                    }
                })
    }

    override fun publishVideo(sign: String?, coverPath: String?, videoPath: String?) {
        mView?.showLoading()
        publish("$sign", "$coverPath", "$videoPath")
    }

    /**
     * 上报到服务器
     */
    override fun reportServer(desc: String?, videoUrl: String?, coverUrl: String?, videoId: String?, time: String?) {
            mPublishCall = ApiManager.get().getApiService(VideoService::class.java)
                    .postVideo("$desc", "$videoUrl", "$coverUrl", "$videoId", "$time")
            HttpRequest.getRequest().autoRecycle(mView?.getContext())
                    .start(mPublishCall, object: ResponseCallback<Bonus> () {
                        override fun onSuccess(url: String?, data: Bonus?) {
                            mView?.hideLoading()
                            mView?.reportServerSuccess(data)
                        }

                        override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                            super.onError(url, errorCode, errorMsg)
                            mView?.hideLoading()
                        }
                    })
    }
    /**
     * 腾讯云上传
     */
    private fun publish(sign: String?,
                        coverPath: String?,
                        videoPath: String?) {
        val txugcPublish = VideoPublish(CommonModule.getApplication(), "customID")
        txugcPublish.setListener(object : TXUGCPublishTypeDef.ITXVideoPublishListener {
            override fun onPublishProgress(uploadBytes: Long, totalBytes: Long) {

            }

            override fun onPublishComplete(result: TXUGCPublishTypeDef.TXPublishResult) {
                if(!TextUtils.isEmpty(result.videoURL) ) {
                    mView?.publishSuccess(result)
                } else {
                    mView?.publishError(101, "上传文件失败~")
                }
            }
        })

        val param = TXUGCPublishTypeDef.TXPublishParam()
        param.signature = sign
        param.videoPath = videoPath
        param.coverPath = coverPath
        txugcPublish.publishVideo(param)
    }

    override fun queryAll() {
        mView?.getVideoList(DBHelper.queryAll(Video::class.java))
    }
}